package
{
	import flash.display.Graphics;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import flash.text.TextFieldAutoSize;
	
	import qnx.ui.buttons.LabelButton;
	import qnx.ui.core.Container;
	import qnx.ui.core.ContainerAlign;
	import qnx.ui.core.ContainerFlow;
	import qnx.ui.core.Containment;
	import qnx.ui.core.SizeMode;
	import qnx.ui.core.SizeUnit;
	import qnx.ui.core.Spacer;
	import qnx.ui.core.UIComponent;
	import qnx.ui.text.KeyboardType;
	import qnx.ui.text.Label;
	import qnx.ui.text.TextInput;

	public class SizeDialog extends UIComponent
	{
		private var dialogContainer:Container;
		private var widthText:TextInput;
		private var heightText:TextInput;
		private var oldWidth:String;
		private var oldHeight:String;
		private var mainGame:LifeGame;
		
		public function SizeDialog(main:Object)
		{	
			mainGame = LifeGame(main);
			
			InitializeUI(mainGame);
			
//			widthText.addEventListener(Event.CHANGE, changeWidth);
//			heightText.addEventListener(Event.CHANGE, changeHeight);
			
			oldWidth = String(main.world.columns);
			oldHeight = String(main.world.rows);
		}
		
		private function InitializeUI(main:LifeGame):void
		{
			dialogContainer = new Container();
			dialogContainer.flow = ContainerFlow.VERTICAL;
			dialogContainer.margins = Vector.<Number>([5,5,5,5]);
			dialogContainer.debugColor = 0x00FF00;
			dialogContainer.align = ContainerAlign.MID;
			
			dialogContainer.setSize(420, 220);
			//			dialogContainer.x = (main.stage.fullScreenWidth - dialogContainer.width) / 2;
			//			dialogContainer.y = (main.stage.fullScreenHeight - dialogContainer.height) / 2;
			addChild(dialogContainer);
			
			var myFormat:TextFormat = new TextFormat();
			myFormat.color = 0x000000;
			myFormat.size = 20;	//
			myFormat.bold = true;	// Changed to match Alert Dialog box
			myFormat.font = "Verdana";	//
			myFormat.align = TextFormatAlign.CENTER;
			
			var titleText:Label = new Label();
			titleText.text = "Chang Size";
			titleText.autoSize = TextFieldAutoSize.CENTER;
//			titleText.height = 26;
			titleText.format = myFormat;
			
			var messageText:Label = new Label();
			messageText.text = "Do you want to change the world size?\nOld world will lost.";
			trace("mult:"+messageText.multiline);
			messageText.multiline = true;
			messageText.height = 50;
			messageText.width = dialogContainer.width;
			var myFormat2:TextFormat = new TextFormat();
			myFormat2.align = TextFormatAlign.CENTER;
			messageText.format = myFormat2;
			
			var buttonContainer:Container = new Container();
			buttonContainer.flow = ContainerFlow.HORIZONTAL;
			buttonContainer.margins = Vector.<Number>([20,0,20,0]);
			buttonContainer.size = 40;
			buttonContainer.sizeUnit = SizeUnit.PIXELS;
			
			var OKButton:LabelButton = new LabelButton();
			OKButton.label = "OK";
			OKButton.addEventListener(MouseEvent.CLICK, main.OKClicked);
			
			var cancelButton:LabelButton = new LabelButton();
			cancelButton.label = "Cancel";
			cancelButton.addEventListener(MouseEvent.CLICK, main.cancelDialog);
			
			//			buttonContainer.debugColor = 0xFF0000;
			buttonContainer.addChild(OKButton);
			buttonContainer.addChild(new Spacer());
			buttonContainer.addChild(cancelButton);
			
			var widthContainer:Container = new Container();
			widthContainer.flow = ContainerFlow.HORIZONTAL;
			widthContainer.margins = Vector.<Number>([40,0,30,0]);
			widthContainer.size = 40;
			widthContainer.sizeUnit = SizeUnit.PIXELS;
			
			var widthLabel:Label = new Label();
			widthLabel.text = "Width(1~200)";
			widthLabel.width = 130;
			
			widthText = new TextInput();
			widthText.keyboardType = KeyboardType.PHONE;
			widthText.size = 200;
			widthText.text = String(main.world.columns);
			
			widthContainer.addChild(widthLabel);
			widthContainer.addChild(widthText);
			
			var heightContainer:Container = new Container();
			heightContainer.flow = ContainerFlow.HORIZONTAL;
			heightContainer.margins = Vector.<Number>([40,0,30,0]);
			heightContainer.size = widthContainer.size;
			heightContainer.sizeUnit = widthContainer.sizeUnit;
			
			var heightLabel:Label = new Label();
			heightLabel.text = "Height(1~200)";
			heightLabel.width = widthLabel.width;
			
			heightText = new TextInput();
			heightText.keyboardType = widthText.keyboardType;
			heightText.size = widthText.size;
			heightText.text = String(main.world.rows);
			
			heightContainer.addChild(heightLabel);
			heightContainer.addChild(heightText);
			
			dialogContainer.addChild(titleText);
			dialogContainer.addChild(messageText);
			dialogContainer.addChild(widthContainer);
			dialogContainer.addChild(heightContainer);
			dialogContainer.addChild(new Spacer());
			dialogContainer.addChild(buttonContainer);
			
		}
		
		override protected function draw():void
		{
			trace("draw size dialog");
			var g:Graphics = graphics;
			g.clear();
			g.beginFill(0x000000, .8);			// Changed to match Alert Dialog box
			g.drawRect(0, 0, mainGame.stage.fullScreenWidth, mainGame.stage.fullScreenHeight);
			g.endFill();
			
			dialogContainer.x = (mainGame.stage.fullScreenWidth - dialogContainer.width) / 2;
			dialogContainer.y = (mainGame.stage.fullScreenHeight - dialogContainer.height) / 2;
			
			dialogContainer.layout();
			dialogContainer.graphics.clear();
			dialogContainer.graphics.beginFill(0xEDEDED,1);
			dialogContainer.graphics.drawRoundRect(0, 0, dialogContainer.width, dialogContainer.height, 10);
			dialogContainer.graphics.endFill();
		}
		
		private function changeWidth(event:Event):Boolean
		{
			var ret:Boolean = isValidUint(widthText.text);
			if(ret)
			{
				oldWidth = widthText.text;
			}
			else
			{
				// output an error
				widthText.text = oldWidth;
			}
			return ret;
		}
		
		private function changeHeight(event:Event):Boolean
		{
			var ret:Boolean = isValidUint(heightText.text);
			if(ret)
			{
				oldHeight = heightText.text;
			}
			else
			{
				// output an error
				heightText.text = oldHeight;
			}
			return ret;
		}
		
		private function isValidUint(input:String):Boolean
		{
			var num:Number = Number(input);
			if(String(num) != input)
				return false;
			
			if(!isNaN(num) && uint(num) == num)
			{
				if(num<1 || num>200)
					return false;
				else
					return true;
			}
			else
				return false;				
		}
		
		public function changeSizeNumber():void
		{
			changeWidth(null);
			changeHeight(null);
		}
		
		public function getWidthNumber():int
		{
			return int(oldWidth);
		}
		
		public function getHeightNumber():int
		{
			return int(oldHeight);
		}
	}
}